home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1373 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  59 lines

  1. Path: ts1-011.jaxnet.com!user
  2. From: garyg@jax.jaxnet.com (Gary M. Greenberg)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: printf - adding commas to numbers
  5. Date: Fri, 12 Jan 1996 23:24:51 -0500
  6. Organization: Southeast Network Services, Inc.
  7. Message-ID: <garyg-1201962324510001@ts1-011.jaxnet.com>
  8. References: <4d15eq$lc5@ixnews2.ix.netcom.com> <4d3b5l$fsm@news.infi.net> <4d66rt$cc0@cloner3.netcom.com>
  9. NNTP-Posting-Host: ts1-011.jaxnet.com
  10.  
  11. Chuck,
  12.  
  13. It ain't printf as you requested; you could probably write a real simple
  14. function that counts the strlen that you want ","ed, and walk the string <of
  15. numbers> as they will surely be arrayable.
  16. Then, starting from the end of that array, "number[strlen(number)]",
  17. count down and insert a comma every third one ... here, an illustration
  18. ;-)
  19.  
  20. /* commafy.c */
  21.  
  22. /*
  23. ** function to insert ","s every third 
  24. ** character for formatting numbers
  25. */
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29.  
  30. #define STRLEN 120
  31.  
  32. int main ( )
  33. {
  34.     /*** simple test with hardwired input ***/
  35.     char number[STRLEN]="987654321987654321";
  36.     int i;
  37.     
  38.     for(i=strlen(number);i>0;i--) {
  39.         putchar(number[strlen(number)-i]);
  40.         if(i%3==1 && i>1 && i<=strlen(number))
  41.             putchar(',');
  42.     }
  43.     return 0;
  44. }
  45.  
  46.  
  47. Not real elegant, _but_
  48.  
  49.     "It's tired and I'm getting late,
  50.     "I want to try to sleep but I can't relate ..."
  51.                                 -- SeaTrain.
  52.  
  53. C'ya,
  54.  
  55. gary /* the Sorcerer's Apprentice */
  56.  
  57. "Why do we have to hide from the police, Daddy?" | Truth:
  58. "Because we use vi, honey. They use emacs."      | This .sig is pirated
  59.